package com.xhwl.centiotdata.handler.vehicle.impl;


import com.xhwl.centiotdata.converter.vehicle.VehicleAccessRecordThirdConverter;
import com.xhwl.centiotdata.converter.vehicle.VehiclePayRecordThirdConverter;
import com.xhwl.centiotdata.handler.vehicle.IVehicleIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.fushi.VehicleInOutRecordFuShiDTO;
import com.xhwl.centiotdata.pojo.dto.fushi.VehiclePayRecordFuShiDTO;
import com.xhwl.centiotdata.service.cent.IVehicleCentService;
import com.xhwl.centiotdata.suppliersystem.vehicle.FuShi.FuShiHandler;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordsForInFeignDTO;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordsForOutFeignDTO;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleIntegrationConfigBO;
import com.xhwl.common.pojo.cent.vehicle.bo.feign.CreateVehicleAccessRecordsForInFeignBO;
import com.xhwl.common.pojo.cent.vehicle.bo.feign.CreateVehicleAccessRecordsForOutFeignBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.*;
import com.xhwl.common.pojo.cent.vehicle.bo.http.query.QueryAccessRecordBO;
import com.xhwl.common.pojo.cent.vehicle.converter.feign.AccessRecordFeignServiceConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 对接富士
 */
@Slf4j
@Service
public class FuShiIntegrationHandler implements IVehicleIntegrationHandler {

    @Autowired
    private FuShiHandler fuShiHandler;

    @Autowired
    private VehicleAccessRecordThirdConverter vehicleAccessRecordThirdConverter;

    @Autowired
    private VehiclePayRecordThirdConverter vehiclePayRecordThirdConverter;

    @Autowired
    private AccessRecordFeignServiceConverter accessRecordFeignServiceConverter;

    @Autowired
    private IVehicleCentService vehicleCentService;

    /**
     * 查询进记录
     *
     * @param vehicleIntegrationConfigBO
     * @param queryAccessRecordBO
     * @return
     */
    @Override
    public ParkingLotAccessRecordForInBO queryAccessRecordForIn(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, QueryAccessRecordBO queryAccessRecordBO) {
        List<VehicleInOutRecordFuShiDTO> vehicleInOutRecordFuShiDTOS = fuShiHandler.pullInRecord(vehicleIntegrationConfigBO, queryAccessRecordBO);
        ParkingLotAccessRecordForInBO parkingLotAccessRecordForInBO = new ParkingLotAccessRecordForInBO();
        if (!vehicleInOutRecordFuShiDTOS.isEmpty()) {
            List<AccessRecordForInBO> accessRecordForInBOS = vehicleAccessRecordThirdConverter.fuShiList2AccessRecordForInBOList(vehicleInOutRecordFuShiDTOS,
                    vehicleIntegrationConfigBO);
            parkingLotAccessRecordForInBO.setAccessRecordForInBOList(accessRecordForInBOS);
        }
        return parkingLotAccessRecordForInBO;
    }

    @Override
    public ParkingLotPayRecordBO queryPayRecord(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, QueryAccessRecordBO queryAccessRecordBO) {
        List<VehiclePayRecordFuShiDTO> vehiclePayRecordFuShiDTOS = fuShiHandler.pullPayRecord(vehicleIntegrationConfigBO, queryAccessRecordBO);
        ParkingLotPayRecordBO parkingLotPayRecordBO = new ParkingLotPayRecordBO();
        if (!vehiclePayRecordFuShiDTOS.isEmpty()) {
            List<PayRecordBO> payRecordBOList = vehiclePayRecordThirdConverter.fuShiList2PayRecordBOList(vehiclePayRecordFuShiDTOS,
                    vehicleIntegrationConfigBO);
            parkingLotPayRecordBO.setPayRecordBOList(payRecordBOList);
        }
        return parkingLotPayRecordBO;
    }

    /**
     * 查询出记录
     *
     * @param vehicleIntegrationConfigBO
     * @param queryAccessRecordBO
     * @return
     */
    @Override
    public ParkingLotAccessRecordForOutBO queryAccessRecordForOut(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, QueryAccessRecordBO queryAccessRecordBO) {
        List<VehicleInOutRecordFuShiDTO> vehicleInOutRecordFuShiDTOS = fuShiHandler.pullOutRecord(vehicleIntegrationConfigBO, queryAccessRecordBO);
        ParkingLotAccessRecordForOutBO parkingLotAccessRecordForOutBO = new ParkingLotAccessRecordForOutBO();
        if (!vehicleInOutRecordFuShiDTOS.isEmpty()) {
            List<AccessRecordForOutBO> accessRecordForOutBOS = vehicleAccessRecordThirdConverter.fuShiList2AccessRecordForOutBOList(vehicleInOutRecordFuShiDTOS,
                    vehicleIntegrationConfigBO);
            parkingLotAccessRecordForOutBO.setAccessRecordForOutBOList(accessRecordForOutBOS);
        }
        return parkingLotAccessRecordForOutBO;
    }

    /**
     * 将进记录加到车行中台
     *
     * @param createVehicleAccessRecordsForInFeignBO
     */
    @Override
    public void addAccessRecordsForIn(CreateVehicleAccessRecordsForInFeignBO createVehicleAccessRecordsForInFeignBO) {
        log.info("start: Fu Shi add access record for in");
        CreateVehicleAccessRecordsForInFeignDTO createVehicleAccessRecordsForInFeignDTO = accessRecordFeignServiceConverter.toCreateVehicleAccessRecordsForInFeignDTO(createVehicleAccessRecordsForInFeignBO);

        try {
            vehicleCentService.addAccessRecordsForIn(createVehicleAccessRecordsForInFeignDTO);
            log.info("end:Fu Shi add access record for in");
        } catch (Exception e) {
            log.error("iot调用车行中台的新增入场记录的接口，发生错误，iot的数据无法给到车行中台");
        }
    }

    /**
     * 将出记录添加到车行中台
     *
     * @param createVehicleAccessRecordsForOutFeignBO
     */
    @Override
    public void addAccessRecordsForOut(CreateVehicleAccessRecordsForOutFeignBO createVehicleAccessRecordsForOutFeignBO) {
        log.info("start: Fu Shi add access record for out");
        CreateVehicleAccessRecordsForOutFeignDTO createVehicleAccessRecordsForOutFeignDTO = accessRecordFeignServiceConverter.toCreateVehicleAccessRecordsForOutFeignDTO(createVehicleAccessRecordsForOutFeignBO);

        try {
            vehicleCentService.addAccessRecordsForOut(createVehicleAccessRecordsForOutFeignDTO);
            log.info("end: Fu Shi add access record for out");
        } catch (Exception e) {
            log.error("iot调用车行中台的新增出场记录的接口，发生错误，iot的数据无法给到车行中台");
        }
    }


    @Override
    public void addPayRecords(ParkingLotPayRecordBO parkingLotPayRecordBO) {
        log.info("start: Fu Shi add pay record");

        try {
            vehicleCentService.addPayRecords(parkingLotPayRecordBO);
            log.info("end: Fu Shi add pay record ");
        } catch (Exception e) {
            log.error("iot调用车行中台的新增缴费记录的接口，发生错误，iot的数据无法给到车行中台");
        }
    }
}
